﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BA2009.DataAccess.Context;
using BA2009.DataAccess.Entities;
using System.Data.Linq;
using BA2009.Data.Filters;

namespace BA2009.Data.DataAccess
{
    public class ResourceRepository:RepositoryBase<Entities.Resource>,IResourceRepository
    {
         private MapTablesRepository ResourcebannerRepository = new MapTablesRepository("ResourceID", "BannerID", "BannerResource");
         
         public override IQueryable<Entities.Resource> FindAll()
        {
            var listResource = from r in db.Resources
                               select new BA2009.Entities.Resource
                               {
                                   ResourceID = r.ResourceID,
                                   Code = r.Code,
                                   Description = r.Description,
                                   FileName = r.FileName,
                                   NumberOfClicked = r.NumberOfClicked,
                                   ResourceTypeID = r.ResourceTypeID
                               };

            return listResource;
        }

        public override Entities.Resource GetById(int id)
        {
             var resource= this.FindAll().ResourceFilterById(id).SingleOrDefault();

             var banners= (from b in db.Banners
                           where b.BannerID== id
                           select b).ToList();
             IList<int> bnrs= new List<int>();

             if (banners!= null && banners.Count>0)
             {
                  foreach (var b in banners)
                  {
                       bnrs.Add(b.BannerID);
                  }
             }
             resource.Banners= bnrs;

             return resource;

        }

        protected override void Delete(int id, BA2009.DataAccess.Context.BA2009Context dbContext)
        {
             //Delete all BannerID in BannerResources table with BannerId.
             var banners = (from b in db.BannerResources
                            where b.BannerID== id
                            select b).AsEnumerable();

             dbContext.BannerResources.DeleteAllOnSubmit(banners);
             dbContext.SubmitChanges();

             //Delete Resource with id is selected.
             var resource = (from res in db.Resources
                             where res.ResourceID== id
                             select res).SingleOrDefault();
             dbContext.Resources.DeleteOnSubmit(resource);
             dbContext.SubmitChanges();


        }

        protected override object GetLinqEntity(int id)
        {
             return (from res in db.Resources
                     where res.ResourceID== id
                     select res).SingleOrDefault();
        }

        protected override void Save(object linqEnity, BA2009.DataAccess.Context.BA2009Context dbContext, params object[] maps)
        {
             var resoure= linqEnity as Resource;

             dbContext.Resources.InsertOnSubmit(resoure);
             dbContext.SubmitChanges();

             if (maps.Length != 2)
             {
                  throw new InvalidOperationException("Tham số truyền vào hàm Save của ResourceRepository sai.");
             }

             IList<Entities.MapEntity> banners = maps [ 0 ] as IList<Entities.MapEntity>;
             if (banners != null)
             {

                  foreach (var banner in banners)
                  {
                       banner.ID1 = resoure.ResourceID;
                  }

                  ResourcebannerRepository.Insert(banners, dbContext);
             }
             dbContext.SubmitChanges();

        }

        protected override void Update(object itemToUpdate, object orginialItem, BA2009.DataAccess.Context.BA2009Context dbContext, params object[] maps)
        {
             if (maps.Length != 2)
             {
                  throw new InvalidOperationException("Tham số truyền vào hàm Update của ResourceRepository sai.");
             }

             //Update Resources table.
             var entity = itemToUpdate as Resource;
             var original = orginialItem as Resource;
             dbContext.Resources.Attach(entity, original);
             dbContext.SubmitChanges();

             // Update BannerResources table.
             IList<Entities.MapEntity> banners = maps [ 0 ] as IList<Entities.MapEntity>;
             if (banners != null)
             {
                  ResourcebannerRepository.Update(banners, dbContext);
             }
             dbContext.SubmitChanges();
        }
    }
}
